package chapter07;

import java.util.HashSet;

public class Test02_HashSet {
    public static void main(String[] args) {
        //新建
        HashSet s = new HashSet();
        //新增元素 无序新增 所有和下标有关的操作都失效了
        s.add("张三");
        s.add("李四");
        s.add("王五");
        s.add("赵六");
        System.out.println(s);
        //set中的元素不能重复
        s.add("张三");
        System.out.println(s);
        //删除元素
        s.remove("张三");
        System.out.println(s);
        //查询
        System.out.println(s.contains("李四"));
        //修改 没办法直接修改 只能删除之后再加入
        s.remove("李四");
        s.add("田七");
        System.out.println(s);
        HashSet s2 = new HashSet();
        s2.add(new Student("110","张三"));
        System.out.println(s2);
        s2.add(new Student("110","张三"));
        System.out.println(s2);
    }
}
class Student{
    private String id;
    private String name;
    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return id+":"+name;
    }
    //重新equals方法
    @Override
    public boolean equals(Object obj) {
        if(obj==this) return true;
        if(obj instanceof Student){
            Student s = (Student) obj;
            return s.id.equals(this.id);
        }
        return false;
    }
    @Override
    public int hashCode() {
        return this.id.hashCode();
    }
}
